package com.yuandian.management.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuandian.api.management.entity.ConfDocumentExportInfo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.security.annotation.Inner;
import com.yuandian.management.service.ConfDocumentExportInfoService;
import com.yuandian.po.DocumentExportApplicationForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * -Controller
 * @author zfsn
 * @date 2023-5-11
 */
@Api(tags = "异步下载功能接口")
@RestController
@RequestMapping("/documentExportInfo")
public class AsynchronousDownloadController {

	@Autowired
	private ConfDocumentExportInfoService confDocumentExportInfoService;

	/**
	 *  1、提交文档导出申请
	 * @param documentExportApplicationForm
	 * @return
	 */
	@PostMapping("/documentExport")
	@ApiOperation(value = "提交文档导出申请", notes = "提交文档导出申请")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "serviceName", value = "服务名", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "moduleName", value = "模块名", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "exportButtomName", value = "文档下载按钮名称", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "docExportParameterJson", value = "文档导出，服务端处理文档所需参数，参数格式：默认使用json格式", paramType = "body", required = false, dataType = "Json"),
	})
	public R submitDocumentExportApplication(@RequestBody @ApiIgnore DocumentExportApplicationForm documentExportApplicationForm) {
		return confDocumentExportInfoService.submitDocumentExportApplication(documentExportApplicationForm);
	}


	/**
	 * 2、查看文档导出申请，服务端处理进程,支持分页查询
	 * @param confDocumentExportInfo
	 * @return
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "serviceName", value = "服务名", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "moduleName", value = "模块名", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "isAsc", value = "排序的方向值只能是 desc 或者 asc", paramType = "body", required = false, dataType = "String")
	})
	@PostMapping("/queryDocumentExportApplications")
	@ApiOperation(value = "查看文档导出申请，服务端处理进程,支持分页查询", notes = "查看文档导出申请，服务端处理进程,支持分页查询")
	public R queryDocumentExportApplications(@RequestBody @ApiIgnore ConfDocumentExportInfo confDocumentExportInfo) {
		return confDocumentExportInfoService.queryDocumentExportApplications(confDocumentExportInfo);
	}

	/**
	 * 3、查询文档导出数据
	 * @param confDocumentExportInfo
	 * @return
	 */
	@PostMapping("/list")
	@Inner(value = false)
	@ApiOperation(value = "查询文档导出数据", notes = "查询文档导出数据")
	public R<List<ConfDocumentExportInfo>> list(@RequestBody ConfDocumentExportInfo confDocumentExportInfo) {
		return R.ok(confDocumentExportInfoService.list(Wrappers.query(confDocumentExportInfo)));
	}

	/**
	 * 4、上报文件下载状态
	 * @param confDocumentExportInfo
	 * @return
	 */
	@PostMapping("/uploadDocumentExportInfo")
	@Inner(value = false)
	@ApiOperation(value = "上报文件下载状态", notes = "上报文件下载状态")
	public R uploadDocumentExportInfo(@RequestBody ConfDocumentExportInfo confDocumentExportInfo) {
		return confDocumentExportInfoService.uploadDocumentExportInfo(confDocumentExportInfo);
	}


	/**
	 * 4、下载文件，(支持下载文件后，删除文件)
	 * @param fileName
	 * @return
	 */
	@PostMapping("/deleteDocumentHandleInfoByFileName/{fileName}")
	@Inner(value = false)
	@ApiOperation(value = "下载文件，(支持下载文件后，删除文件)", notes = "下载文件，(支持下载文件后，删除文件)")
	public R deleteDocumentHandleInfoByFileName(@PathVariable("fileName") String fileName) {
		LambdaUpdateWrapper<ConfDocumentExportInfo> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(ConfDocumentExportInfo::getExportFileName, fileName);
		return R.ok(confDocumentExportInfoService.remove(updateWrapper));
	}

}
